package com.sky.controller.admin;

import com.sky.dto.ServiceProviderDTO;
import com.sky.dto.ServiceProviderPageQueryDTO;
import com.sky.entity.ServiceProvider;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ServiceProviderService;
import com.sky.vo.ServiceProviderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 服务人员管理
 */
@RestController
@RequestMapping("/admin/service-provider")
@Api(tags = "服务人员相关接口")
@Slf4j
public class ServiceProviderController {

    @Resource
    private ServiceProviderService serviceProviderService;
    
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增服务人员
     * @param serviceProviderDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增服务人员")
    public Result<String> save(@RequestBody ServiceProviderDTO serviceProviderDTO) {
        log.info("新增服务人员：{}", serviceProviderDTO);
        serviceProviderService.addWithSkill(serviceProviderDTO);
        // 清理缓存
        String key = "service_provider_" + serviceProviderDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 服务人员分页查询
     * @param serviceProviderPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("服务人员分页查询")
    public Result<PageResult> page(ServiceProviderPageQueryDTO serviceProviderPageQueryDTO) {
        log.info("服务人员分页查询：{}", serviceProviderPageQueryDTO);
        PageResult pageResult = serviceProviderService.pageQuery(serviceProviderPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除服务人员
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除服务人员")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("批量删除服务人员：{}", ids);
        serviceProviderService.deleteBatch(ids);
        // 清理缓存
        cleanCache("service_provider_*");
        return Result.success();
    }

    /**
     * 根据id查询服务人员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询服务人员")
    public Result<ServiceProviderVO> getById(@PathVariable Long id) {
        log.info("根据id查询服务人员：{}", id);
        ServiceProviderVO serviceProviderVO = serviceProviderService.getServiceProviderVOById(id);
        return Result.success(serviceProviderVO);
    }

    /**
     * 修改服务人员
     * @param serviceProviderDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改服务人员")
    public Result<String> update(@RequestBody ServiceProviderDTO serviceProviderDTO) {
        log.info("修改服务人员：{}", serviceProviderDTO);
        serviceProviderService.updateWithSkill(serviceProviderDTO);
        // 清理缓存
        cleanCache("service_provider_*");
        return Result.success();
    }

    /**
     * 启用停用服务人员
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用停用服务人员")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用停用服务人员：{},{}", status, id);
        serviceProviderService.startOrStopProvider(status, id);
        // 清理缓存
        cleanCache("service_provider_*");
        return Result.success();
    }

    /**
     * 根据分类id查询服务人员
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询服务人员")
    public Result<List<ServiceProvider>> list(Long categoryId) {
        List<ServiceProvider> list = serviceProviderService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 根据手机号查询服务人员
     * @param phone
     * @return
     */
    @GetMapping("/getByPhone")
    @ApiOperation("根据手机号查询服务人员")
    public Result<ServiceProvider> getByPhone(String phone) {
        ServiceProvider serviceProvider = serviceProviderService.getByPhone(phone);
        return Result.success(serviceProvider);
    }

    /**
     * 根据用户名查询服务人员
     * @param username
     * @return
     */
    @GetMapping("/getByUsername")
    @ApiOperation("根据用户名查询服务人员")
    public Result<ServiceProvider> getByUsername(String username) {
        ServiceProvider serviceProvider = serviceProviderService.getByUsername(username);
        return Result.success(serviceProvider);
    }
    
    /**
     * 清理缓存
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
} 